﻿using System.Diagnostics;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Macad.Core;
using Macad.Core.Topology;
using Macad.Interaction;
using Macad.Interaction.Dialogs;
using Macad.Presentation;
using SpCad;
using SpCad.Core.Modify;
using SpCad.Utils;
using SPCAM.Plugin.Cam.Tools;
using SPCAM.Servers;
using RelayCommand = Macad.Presentation.RelayCommand;

namespace SPCAM.Utils
{
    public partial class MainCommands : ObservableObject
    {
        ISpView spview;
        public RelayCommand DoRedoCommand { get; }
        public RelayCommand DoUndoCommand { get; }
  

        public MainCommands(ISpView cc)
        {
            spview = cc;
            DoRedoCommand = new RelayCommand(_DpRedo, CanRedo);
            DoUndoCommand = new RelayCommand(DoUndo, CanDoUndo);
     
        }

        private void WorkspaceController_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            string name = e.PropertyName;
            if(e.PropertyName is not "CursorPosition" and not "CursorPosition2d" and not "CursorPosition")
            {
                string acc = e.PropertyName;
            }
            if(e.PropertyName is "CurrentTool")
            {
                var aa = (sender as WorkspaceController).CurrentTool;
                var p = "CurrentTool";
            }
        }

       
        /// <summary>
        /// 导入零件
        /// </summary>
        [RelayCommand]
        private void Import()
        {

            spview.Import();
        }
        private bool CanDoUndo() {

           return InteractiveContext.Current?.UndoHandler?.CanUndo ?? false;

        }

        private bool CanRedo() => InteractiveContext.Current?.UndoHandler?.CanRedo ?? false;
      
        private void DoUndo()
        {
            // InteractiveContext.Current?.UndoHandler?.CanUndo ?? false
            if (InteractiveContext.Current.WorkspaceController.PrepareUndo())
            {
                InteractiveContext.Current.UndoHandler.DoUndo(1);
                InteractiveContext.Current.WorkspaceController.Invalidate();
            }

        }
        
        [RelayCommand(CanExecute =nameof(CanDoUndo))]
        private void _DpRedo()
        {  // InteractiveContext.Current?.UndoHandler?.CanRedo ?? false
            if (InteractiveContext.Current.WorkspaceController.PrepareUndo())
            {
                InteractiveContext.Current.UndoHandler.DoRedo(1);
                InteractiveContext.Current.WorkspaceController.Invalidate();
            }
        }
        
        /// <summary>
        /// 缩放所有
        /// </summary>
        [RelayCommand]
        private void ZoomFitAll()
        {
            InteractiveContext.Current?.ViewportController?.ZoomFitAll();
        }
        /// <summary>
        /// 缩小
        /// </summary>
        [RelayCommand]
        private void ZoomOut()
        {
            InteractiveContext.Current?.ViewportController?.Zoom(-0.5);
        }
        /// <summary>
        /// 放大
        /// </summary>
        [RelayCommand]
        private void ZoomIn()
        {
            InteractiveContext.Current?.ViewportController?.Zoom(0.5);
        }
        /// <summary>
        /// 显示模式
        /// </summary>
        /// <param name="pr"></param>
        [RelayCommand]
        private void ShowType(string pr)
        {
            Viewport.RenderModes vname = Viewport.RenderModes.SolidShaded;
            switch (pr)
            {
                case "SolidShaded":
                    vname = Viewport.RenderModes.SolidShaded;
                    break;
                case "HLR":
                    vname = Viewport.RenderModes.HLR;
                    break;
                case "Raytraced":
                    vname = Viewport.RenderModes.Raytraced;
                    break;

                default:
                    break;
            }
            var viewport = InteractiveContext.Current?.ViewportController?.Viewport;
            if (viewport != null)
            {
                viewport.RenderMode = vname;
                InteractiveContext.Current?.WorkspaceController.Invalidate();
            }


        }
        /// <summary>
        /// 显示Shape
        /// </summary>
        /// <param name="pr"></param>
        [RelayCommand ]
        private void ShowShape()
        {
            MainWindow.Current?.Docking.ActivateToolAnchorable("ShapeInspector");
            // 选择事件InteractiveContext.Current.WorkspaceController.Selection.SelectionChanged
           var cd=InteractiveContext.Current.WorkspaceController.Selection;
            //var ad = InteractiveContext.Current;
            var body = InteractiveContext.Current.WorkspaceController.Selection.SelectedEntities.FirstOrDefault() as Body;
            if (body == null)
            {
                Messages.Error("请选择实体后操作.");
                return;
            }
            var cda= body.GetBRep();
            var edges=cda.Edges();
          
          
        }
        [RelayCommand]
        private void OpenFile()
        {
            spview.File.OpenFile();
        }
        [RelayCommand]
        private void SaveFile()
        {
            spview.File.SaveFile();
        }
        [RelayCommand]
        private void NewFile()
        {
            spview.File.NewFile();
        }
     
        [RelayCommand]
        private void SelectEdge()
        {
            try
            {
                InteractiveContext.Current.Workspace.V3dViewer.Redraw();              
                var modifierShape = Weld.Create(InteractiveContext.Current.WorkspaceController.Selection.SelectedEntities.First() as Body);
            
                if (modifierShape != null)
                {
                  
                    //InteractiveContext.Current?.UndoHandler.Commit();
                    InteractiveContext.Current.WorkspaceController.StartTool(new SelectEdge(modifierShape));
                    
                    SpCad.SpServer.GetServer().StartTools(new SelectEdge(modifierShape));
                    //  InteractiveContext.Current.WorkspaceController.StartTool(new FilletEditorTool(modifierShape));
                 
                }
            }
            catch (Exception)
            {

                Debug.Write("err");
            }

            //  InteractiveContext.Current.WorkspaceController.Invalidate();
        }

   

        [RelayCommand]
        private void Test1()
        {
            spview.Context.ViewportController.LockedToPlane = true;
        }
        [RelayCommand]
        private void SelectFace()
        {
  
            try
            {
          
         
                var body = spview.Context.WorkspaceController.Selection.SelectedEntities.FirstOrDefault() as Body;
                if (body == null)
                {
                    Messages.Error("请选择一个实体.");
                    return;
                } 
                InteractiveContext.Current.WorkspaceController.StartTool(new SelectFasceTools(body));
              
            }
            catch (Exception)
            {

                spview.Info("发生错误,可能没有选择");
            }
       
        }
        [RelayCommand]
        private void Tqhf()
        {
            Server.Instance.CamHeadView = true;
        }
        [RelayCommand]
        private void Test3()
        {
            Server.Instance.CamHeadView = true;
        }
        [RelayCommand]
        private void ExitCam()
        {
            Server.Instance.CamHeadView = false;

        }
    }
    }
